<?php

    class KegiatanNonRutin extends CI_Model
    {
        private $activityID;
        private $activityName;
        private $roomID;
        private $typeID;
        private $startDate;
        private $endDate;
        private $status;
        private $PJKegiatan;

        public function KegiatanNonRutin_model() {
                parent::Model();
        }

        //mengambil satu row dari tabel kegiatan non rutin berdasarkan parameter activityID
        public function getKegiatanNonRutin($activityID)
        {
            $query = $this->db->get_where('kegiatannonrutin',array('activityID'=>$activityID));
            return $query->row();
        }

        public function getAllKegiatanNonRutin($userID)
        {
            $query = $this->db->get_where('kegiatannonrutin',array('pjKegiatan'=>$userID));
            return $query->result();
        }

        //Update by : Arian, kalau status bukan direject.. Keterangan : 0=reserved, 1=approved, 2=selesai 3=rejected?
        //Return : ruangan yang statusnya bentrok dengan startDate dan endDate (betul kan,Vin?)
        public function getRoomStatus($roomID,$startDate,$endDate)
        {
            $query = $this->db->query
                    ("
                        SELECT *
                        FROM kegiatannonrutin
                        WHERE (roomID = '".$roomID."') AND
                              (((startDate <= '".$startDate."') AND ('".$startDate."' < endDate)) OR
                               ((startDate < '".$endDate."') AND ('".$endDate."' <= endDate)) OR
                               (('".$startDate."' <= startDate) AND (startDate < '".$endDate."')) OR
                               (('".$startDate."' < endDate) AND (endDate <= '".$endDate."'))
                              )
                              AND
                              (
                                (status=0) OR (status=1)
                              )
                    ");
            return $query->result();
        }

        public function getJadwal($userID,$startDate,$endDate)
        {
            //$query=$this->db->query("SELECT * FROM KEGIATAN");
            $query = $this->db->query
                    ("
                        SELECT *
                        FROM kegiatannonrutin
                        WHERE activityID IN
                        (
                            SELECT DISTINCT pesertanonrutin.activityID
                            FROM pesertanonrutin
                            WHERE (userID = '".$userID."')
                        ) AND
                            (status = 1) AND
                            (((startDate <= '".$startDate."') AND ('".$startDate."' < endDate)) OR
                             ((startDate < '".$endDate."') AND ('".$endDate."' <= endDate)) OR
                             (('".$startDate."' <= startDate) AND (startDate < '".$endDate."')) OR
                             (('".$startDate."' < endDate) AND (endDate <= '".$endDate."'))
                            )
                    ");
            return $query->result();
        }

        public function getAllAvailableKegiatan($start,$end)
        {
            $query = $this->db->get('kegiatannonrutin');
            return $query->result();
        }

        //TODO : pjKegiatan harusnya diambil dari session
        //Return true bila transaksi berhasil
        public function insertKegiatan($activityName,$roomID,$typeID,$startDateTime,$endDateTime,$status,$pjKegiatan)
        {
            $data =array(
                'activityName' =>$activityName,
                'roomID' =>$roomID,
                'typeID' => $typeID,
                'startDate' =>$startDateTime,
                'endDate' => $endDateTime,
                'status' => $status,
                'pjKegiatan' => $pjKegiatan
            );
            return $this->db->insert('kegiatannonrutin',$data);
        }

        //Return true bila transaksi berhasil
        public function updateKegiatan($activityID,$activityName,$roomID,$typeID,$startDateTime,$endDateTime,$status,$pjKegiatan)
        {
            $data =array(
                'activityName' =>$activityName,
                'roomID' =>$roomID,
                'typeID' => $typeID,
                'startDate' =>$startDateTime,
                'endDate' => $endDateTime,
                'status' => $status,
                'pjKegiatan' => $pjKegiatan
            );
            $this->db->where('activityID', $activityID);
            return $this->db->update('kegiatannonrutin',$data);
        }

        public function deleteKegiatan($activityID)
        {
            $this->db->where('activityID', $activityID);
            return $this->db->delete('kegiatannonrutin');
        }
    }
?>
